package com.mxin.network.interceptor.encrypt;

import com.mxin.common.log.LogA;

import java.io.IOException;
import java.nio.charset.Charset;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RequestEncryptInterceptor implements Interceptor {

    private IEncryptListener iEncrypt;

    public RequestEncryptInterceptor(IEncryptListener iEncrypt) {
        this.iEncrypt = iEncrypt;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Charset charset = Charset.forName("UTF-8");
        String method = request.method().toLowerCase().trim();

        HttpUrl url = request.url();
        /*本次请求的接口地址*/
        String apiPath = url.scheme()+"://"+url.host()+":"+url.port()+url.encodedPath().trim();
        /*如果请求的不是服务端的接口，不加密*/
        if (!iEncrypt.isNeedEncrypt(apiPath)) {
            return chain.proceed(request);
        }

        /*如果请求方式是Get或者Delete，此时请求数据是拼接在请求地址后面的*/
        if (url.encodedQuery() != null) {
            try {
                String queryParamNames = request.url().encodedQuery();
                String newUrl = apiPath + iEncrypt.encryptGetRequest(queryParamNames);
                //构建新的请求
                Request.Builder newRequestBuilder = request.newBuilder().url(newUrl);
                iEncrypt.handlerEncryptRequest(newRequestBuilder);
                request = newRequestBuilder.build();
            } catch (Exception e) {
                e.printStackTrace();
                return chain.proceed(request);
            }
        } else if(request.body() != null){
            //不是Get和Delete请求时，则请求数据在请求体中
            RequestBody requestBody = request.body();

            /*判断请求体是否为空  不为空则执行以下操作*/
            if (requestBody != null) {
                MediaType contentType = requestBody.contentType();
                if (contentType != null) {
                    charset = contentType.charset(charset);
                    /*如果是二进制上传  则不进行加密*/
                    if (contentType.type().toLowerCase().equals("multipart")) {
                        return chain.proceed(request);
                    }
                }

                /*获取请求的数据*/
                try {
                    /*构建新的请求体*/
                    RequestBody newRequestBody = iEncrypt.encryptPostRequest(requestBody);
                    /*构建新的requestBuilder*/
                    Request.Builder newRequestBuilder = request.newBuilder();
                    //根据请求方式构建相应的请求
                    switch (method) {
                        case "post":
                            newRequestBuilder.post(newRequestBody);
                            break;
                        case "put":
                            newRequestBuilder.put(newRequestBody);
                            break;
                    }
                    iEncrypt.handlerEncryptRequest(newRequestBuilder);
                    request = newRequestBuilder.build();
                } catch (Exception e) {
                    LogA.e("加密异常====》"+ e.getMessage());
                    return chain.proceed(request);
                }
            }
        }
        return chain.proceed(request);
    }
}
